﻿using System;
using System.Collections.Generic;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Utils.UI;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditAdministratorGroup : AdminPage, IEditAdministratorGroupView
    {
        private EditAdministratorGroupPresenter _presenter;

        #region IEditAdministratorGroupView Members

        public int GroupID
        {
            get
            {
                if (ViewState["GroupID"] == null)
                    ViewState["GroupID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["GroupID"];
            }
        }


        public void SetGroupID(int id)
        {
            ViewState["GroupID"] = id;
        }


        public string Name
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }


        public DefaultAccess DefaultPageAccess
        {
            get
            {
                return rbAllow.Checked ? DefaultAccess.Allow : DefaultAccess.Deny;
            }
            set
            {
                rbAllow.Checked = value == DefaultAccess.Allow;
                rbDeny.Checked = !rbAllow.Checked;
            }
        }


        public DefaultAccess DefaultCustomAdminPageAccess
        {
            get
            {
                return rbAllowCustomAdminPages.Checked ? DefaultAccess.Allow : DefaultAccess.Deny;
            }
            set
            {
                rbAllowCustomAdminPages.Checked = value == DefaultAccess.Allow;
                rbDenyCustomAdminPages.Checked = !rbAllowCustomAdminPages.Checked;
            }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditAdministratorGroup.xml"; }
        }


        public void SetPermissions(IList<Permission> permissions)
        {
            string html = string.Empty;
            RenderPermissions(ref html, permissions, false);
            litPermissions.Text = html;
        }


        public IList<int> SelectedPermissionIDs
        {
            get
            {
                IList<int> IDs = new List<int>();
                foreach (string id in hidSelectedPermissions.Value.Split(",".ToCharArray()))
                {
                    if (Validation.IsPositiveInteger(id))
                        IDs.Add(int.Parse(id));
                }
                return IDs;
            }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditAdministratorGroupPresenter(this);
            HeadingText = GetText(this, GroupID > 0 ? "EditGroupHeading" : "NewGroupHeading");
            if (Page.IsPostBack)
                return;
            _presenter.Initialize();
            HeadingIcon = litIcon.Text;
            txtName.Focus();
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (_presenter.IsValid)
                Response.Redirect("AdministratorGroups.aspx");
        }

        private void RenderPermissions(ref string html, ICollection<Permission> permissions, bool disableAllPermissions)
        {
            if (permissions.Count == 0)
                return;
            html += "<ul>";
            foreach (Permission permission in permissions)
            {
                html += "<li>";
                html += "<input type=\"checkbox\" id=\"chk" + permission.ID + "\"";
                bool permissionEnabled = _presenter.HasPermission(permission);
                if (permissionEnabled)
                    html += " checked=\"checked\"";
                if (disableAllPermissions)
                    html += " disabled=\"disabled\"";
                html += " />";
                html += "<span>" + GetText(this, "llPermission" + permission.Name) + "</span>";
                RenderPermissions(ref html, permission.SubPermissions, disableAllPermissions | !permissionEnabled);
                html += "</li>";
            }
            html += "</ul>";
        }
    }
}